package com.glink.manage.service.Impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.glink.common.contants.BaseCodeConstants;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.BaseCommonUtils;
import com.glink.manage.common.Constants;
import com.glink.manage.common.JwtUtil;
import com.glink.manage.common.enums.MqttEnum;
import com.glink.manage.domain.*;
import com.glink.manage.dto.barriergate.*;
import com.glink.manage.dto.vehicle.VehicleSyncDTO;
import com.glink.manage.mqtt.MqttProperties;
import com.glink.manage.service.*;
import com.glink.manage.service.barriergate.BarrierGate_LianYi;
import com.glink.manage.vo.barriergate.DeviceLoginVO;
import com.glink.manage.vo.barriergate.DeviceNotifyVO;
import com.glink.manage.vo.barriergate.PersonSyncDetailInfoVO;
import com.glink.manage.vo.barriergate.ResultVO;
import com.glink.manage.vo.barriers.BarriersBaseVO;
import com.glink.manage.vo.mqtt.MqInfoVO;
import com.glink.manage.vo.person.PersonSimpleInfoVO;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;


@Service
@Slf4j
public class BarrierGateServiceImpl implements BarrierGateService {

    
    @Autowired
    private BarriersService barriersService;
    
    @Autowired
    private MqttProperties mqttProperties;
    
    @Autowired
    private ConstructionWorkService constructionWorkService;
    
    @Autowired
    private OrgService orgService;
    
    @Autowired
    private PersonManageService personManageService;
    
    @Autowired
    private PersonAccessRelaService personAccessRelaService;
    
    @Autowired
    private AttendanceService attendanceService;
    
    @Autowired
    private AttachmentService attachmentService;
    
    @Autowired
    private BarrierGate_LianYi lianYi;
    
    @Autowired
    private PersonSyncRecordService personSyncRecordService;
    
    @Autowired
    private TemporaryAdmissionService temporaryAdmissionService;
    
    @Autowired
    private ConstructionWorkAccessMonadService constructionWorkAccessMonadService;
    
    @Autowired
    private BlacklistService blacklistService;
    
    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private VehicleAccessRelaService vehicleAccessRelaService;
    
    /**
     * 设备登录 -- 联义
     * @param dto 登录信息
     * @return 登录参数
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public DeviceLoginVO login_lianYi(DeviceLoginDTO dto) {
        String devSno = dto.getDev_sno();
        DeviceLoginVO deviceLoginVO = new DeviceLoginVO();
        deviceLoginVO.setDev_sno(devSno);
        
        boolean success = false;
        int code = 0;
        String msg = "";
        
        if(StringUtils.isNotBlank(devSno)){
            BarriersBaseVO barriersBaseVO = barriersService.findByFactorySerialNumberAndOnLine(devSno);
            if(Objects.nonNull(barriersBaseVO)){
                success = true;
                code = MqttEnum.RequestCode.SUCCESS.getCode();
                msg = MqttEnum.RequestCode.SUCCESS.getMsg();
                deviceLoginVO.setToken(JwtUtil.generateBarrierGateToken(devSno, ""));
                
                // mqtt信息
                MqInfoVO mqInfoVO = new MqInfoVO();
                mqInfoVO.setHost(mqttProperties.getHost());
                mqInfoVO.setKeepalive(mqttProperties.getKeepalive());
                mqInfoVO.setLogin(mqttProperties.getBarrierGateUserName());
                mqInfoVO.setPassword(mqttProperties.getBarrierGatePassword());
                mqInfoVO.setPort(mqttProperties.getPort());
                mqInfoVO.setQos(mqttProperties.getQos());
                mqInfoVO.setTopic(dto.getDev_sno());
                deviceLoginVO.setMqinfo(mqInfoVO);

                // 设备在线
                barriersService.beOnlineByFactorySerialNumber(devSno);
                
                // 设备登录成功，则下发人员同步信息
                if(!Constants.firstSyncByBarrierGatePersonList.contains(devSno) && StringUtils.isNotBlank(barriersBaseVO.getConstructionWorkObjId())){
                    CompletableFuture.runAsync(() ->{
                        Constants.firstSyncByBarrierGatePersonList.add(devSno);
                        try {
                            issuingPerson(devSno, barriersBaseVO.getBrand(), barriersBaseVO.getConstructionWorkObjId());
                        } catch (GeneralSimpleException e) {
                            log.error("设备：{}，登录后下发人员信息异常，异常信息：", devSno, e);
                        }
                    });
                }
            }else{
                code = MqttEnum.RequestCode.ERROR.getCode();
                msg = MqttEnum.RequestCode.ERROR.getMsg();
            }
        }else{
            code = MqttEnum.RequestCode.DEV_NOT_EXIST.getCode();
            msg = MqttEnum.RequestCode.DEV_NOT_EXIST.getMsg();
        }

        deviceLoginVO.setCode(code);
        deviceLoginVO.setMsg(msg);
        deviceLoginVO.setSuccess(success);
        
        return deviceLoginVO;
    }

    /**
     * 查询人员信息 -- 联义
     *
     * @param dto 查询条件
     * @return 人员信息
     */
    @Override
    public ResultVO syncPerson_lianYi(DeviceSyncPersonDTO dto) {
        ResultVO resultVO = new ResultVO();
        PersonSyncDataPathParamsDTO pathParams = dto.getPath_params();
        String devSno = dto.getDev_sno();
        List<PersonSyncDetailInfoVO> detailInfoVOList = Lists.newArrayList();
        if(Objects.nonNull(pathParams)){
            String workId = barriersService.findWorkIdByFactorySerialNumber(devSno);
            if(StringUtils.isBlank(workId)){
                resultVO.setCode(MqttEnum.RequestCode.DEV_NOT_EXIST.getCode());
                resultVO.setMsg(MqttEnum.RequestCode.DEV_NOT_EXIST.getMsg());
                resultVO.setSuccess(true);
                return resultVO;
            }
            List<String> personList = pathParams.getPerson_list();
            
            detailInfoVOList = findPersonDetailInfo(personList, workId);
            detailInfoVOList = detailInfoVOList == null ? Lists.newArrayList() : detailInfoVOList;
        }
        resultVO.setPerson_list(detailInfoVOList);
        resultVO.setCode(MqttEnum.RequestCode.SUCCESS.getCode());
        resultVO.setMsg(MqttEnum.RequestCode.SUCCESS.getMsg());
        resultVO.setSuccess(true);
        return resultVO;
    }

    /**
     * 查询人员详情
     * @param personList 人员列表
     * @param workId 公测后给你
     * @return
     */
    private List<PersonSyncDetailInfoVO> findPersonDetailInfo(List<String> personList, String workId){
        List<PersonSyncDetailInfoVO> detailInfoVOList = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(personList)){

            // 先获取正式人员信息
            detailInfoVOList = personManageService.findByPersonIdInAndWorkId(personList, workId);

            if(CollectionUtils.isNotEmpty(detailInfoVOList)){
                List<String> collect = detailInfoVOList.stream().map(PersonSyncDetailInfoVO::getPerson_id).collect(Collectors.toList());
                personList.removeAll(collect);

                if(CollectionUtils.isNotEmpty(personList)){
                    // 临时准入
                    detailInfoVOList.addAll(temporaryAdmissionService.findByPersonIdInAndWorkId(personList, workId));
                }
            }else{
                // 临时准入
                detailInfoVOList = temporaryAdmissionService.findByPersonIdInAndWorkId(personList, workId);
            }
        }
        return detailInfoVOList;
    }

    /**
     * 设备反馈同步结果
     *
     * @param notifyDTO 结果信息
     * @return 请求状态
     */
    @Override
//    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public DeviceNotifyVO deviceNotify_lianYi(DeviceNotifyDTO notifyDTO) {
        DeviceNotifyVO deviceNotifyVO = new DeviceNotifyVO();
        boolean status = true;
        if(StringUtils.isNotBlank(notifyDTO.getMethod())){
            if(StringUtils.equals(notifyDTO.getMethod(), MqttEnum.PersonBarrierGate.SYNC_PERSON.getKey())){
                // 人员下发
                List<Integer> successPersonIdList = Lists.newArrayList();
                Map<Integer, String> failPersonIdMap = Maps.newHashMap();
                
                // 用户同步
                String successful = notifyDTO.getSuccessful();
                
                if(StringUtils.isNotBlank(successful)){
                    try {
                        // 使用 Jackson 解析 JSON 数组
                        ObjectMapper objectMapper = new ObjectMapper();
                        List list = objectMapper.readValue(successful, List.class);

                        // 将数字字符串数组转换为整数数组
                        for (Object numberString : list) {
                            successPersonIdList.add(Integer.parseInt((String) numberString));
                        }
                    }catch (Exception e){
                        log.info("人员道闸，人员信息同步状态，异常：{}", successful, e);
                    }
                    
                }

                List<DeviceNotifyFailedDTO> failedList = notifyDTO.getFailed();
                if(CollectionUtils.isNotEmpty(failedList)){
                    failedList.forEach(failed -> {
                        Integer personId = failed.getPerson_id();
                        failPersonIdMap.put(personId, JSON.toJSONString(failed.getInfo()));
                    });
                }

                // 是否进行下次同步
                boolean nextSyncStatus = false;
                if(CollectionUtils.isNotEmpty(successPersonIdList)){
                    List<PersonSyncRecordBean> recordBeanList = Lists.newArrayList();
                    successPersonIdList.forEach(personId -> {
                        PersonSyncRecordBean recordBean = new PersonSyncRecordBean();
                        recordBean.setObjId(BaseCommonUtils.generateUUID());
                        recordBean.setId(personId);
                        recordBean.setSyncStatus(BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
                        recordBean.setDevSno(notifyDTO.getDev_sno());
                        recordBeanList.add(recordBean);
                    });
                    status = personSyncRecordService.saveBatch(recordBeanList);
                    nextSyncStatus = true;
                }

                if(status && !failPersonIdMap.isEmpty()){
                    List<PersonSyncRecordBean> recordBeanList = Lists.newArrayList();
                    
                    failPersonIdMap.forEach((personId, msg) -> {
                        PersonSyncRecordBean recordBean = new PersonSyncRecordBean();
                        recordBean.setObjId(BaseCommonUtils.generateUUID());
                        recordBean.setId(personId);
                        recordBean.setSyncStatus(BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
                        recordBean.setDevSno(notifyDTO.getDev_sno());
                        recordBean.setFailedMsg(msg);
                        recordBeanList.add(recordBean);
                    });
                    status = personSyncRecordService.saveBatch(recordBeanList);
                    
                    nextSyncStatus = true;
                }
                
                if(status && nextSyncStatus){
                    // 下发下一次同步数据
                    issuingPerson(notifyDTO.getDev_sno());
                }else{
                    log.error("行人道闸，接收设备返回的同步信息保存失败:{}-{}", status, nextSyncStatus);
                }
            }else if(StringUtils.equals(notifyDTO.getMethod(), MqttEnum.PersonBarrierGate.DELETE_PERSON.getKey())){
                // 人员删除
                
            }else if(StringUtils.equals(notifyDTO.getMethod(), MqttEnum.PersonBarrierGate.DELETE_ALL.getKey())){
                
            }
        }
        
        if(status){
            deviceNotifyVO.setCode(MqttEnum.RequestCode.SUCCESS.getCode());
            deviceNotifyVO.setMsg(MqttEnum.RequestCode.SUCCESS.getMsg());
        }else{
            deviceNotifyVO.setCode(MqttEnum.RequestCode.DATA_SAVE_FAIL.getCode());
            deviceNotifyVO.setMsg(MqttEnum.RequestCode.DATA_SAVE_FAIL.getMsg());
        }
        deviceNotifyVO.setSuccess(status);
        
        return deviceNotifyVO;
    }

    /**
     * 下发人员到道闸
     * @param devSno 设备号
     */
    @Override
    public void issuingPerson(String devSno){
        log.error("设备：{}，继续下发人员信息", devSno);
        CompletableFuture.runAsync(() -> {
            if (StringUtils.isNotBlank(devSno)) {
                BarriersBaseVO barriersBaseVO = barriersService.findByFactorySerialNumberAndOnLine(devSno);
                if (Objects.nonNull(barriersBaseVO) && StringUtils.isNotBlank(barriersBaseVO.getConstructionWorkObjId())) {
                    try {
                        issuingPerson(devSno, barriersBaseVO.getBrand(), barriersBaseVO.getConstructionWorkObjId());
                    } catch (GeneralSimpleException e) {
                        log.error("设备：{}，继续下发人员信息异常，异常信息：", devSno, e);
                    }
                }
            }
        });
    }
    
    /**
     * 下发人员到道闸
     * @param devSno 设备号
     * @param constructionWorkObjId 工程ID
     */
    private void issuingPerson(String devSno, String brand, String constructionWorkObjId) throws GeneralSimpleException {
        
        ConstructionWorkBean constructionWorkBean = constructionWorkService.getById(constructionWorkObjId);
        if(Objects.nonNull(constructionWorkBean)){
            if(StringUtils.isNotBlank(constructionWorkBean.getProjectDepartmentObjId())){
                List<String> orgIdList = orgService.findOrgTreeIdByOrgId(constructionWorkBean.getProjectDepartmentObjId());

                Date date = DateUtil.beginOfDay(DateUtil.date());
                List<Integer> personIdList = personManageService.findPersonIdByWorkIdAndNotSync(devSno, orgIdList, constructionWorkObjId, date, Constants.PERSON_SYNC_LIMIT);
                if(CollectionUtils.isNotEmpty(personIdList)){
                    List<Integer> allPersonIdList = personManageService.findPersonIdByWorkIdAndNotSync(devSno, orgIdList, constructionWorkObjId, date, null);
                    
                    if(StringUtils.equals(brand, BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)) {
                        lianYi.issuedPersonData_syncPerson(devSno, personIdList, allPersonIdList.size());
                    }
                }
            }
        }
    }

    /**
     * 组装mqtt数据- 人员删除
     * @param personObjId 人员
     */
    public void issuingPersonDataDelete(String personObjId){
        if(StringUtils.isNotBlank(personObjId)){
            PersonManageBean personManageBean = personManageService.getById(personObjId);
            if(Objects.nonNull(personManageBean)) {
                int personId = personManageBean.getId();

                List<String> workIdList = Lists.newArrayList();
                String orgId = personManageBean.getOrgId();
                // 部门直接查工程
                orgId = orgService.findDepartmentOrgIdByOrgId(orgId);
                List<ConstructionWorkBean> constructionWorkBeanList = constructionWorkService.lambdaQuery().eq(ConstructionWorkBean::getProjectDepartmentObjId, orgId).list();
                if(org.apache.commons.collections.CollectionUtils.isNotEmpty(constructionWorkBeanList)){
                    workIdList = constructionWorkBeanList.stream().map(ConstructionWorkBean::getObjId).collect(Collectors.toList());
                }

                // 参建单位查关联的工程
                workIdList.addAll(constructionWorkService.findWorkIdByUnitId(orgId));

                if(CollectionUtils.isNotEmpty(workIdList)){
                    List<BarriersBean> barriersBeanList = barriersService.findFactorySerialNumberByDistinguishAndWorkIdIn(BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02, workIdList);
                    if(CollectionUtils.isNotEmpty(barriersBeanList)){
                        // 联义设备
                        List<String> lianYiDevSnoList = barriersBeanList.stream().filter(obj -> StringUtils.equals(obj.getBrand(), BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)).map(BarriersBean::getFactorySerialNumber).collect(Collectors.toList());
                        lianYi.issuedPersonData_deletePerson(lianYiDevSnoList, personId);
                    }
                }
            }
        }
    }

    /**
     * 组装mqtt数据- 人员删除
     * @param personObjId 人员
     */
    @Override
    public void issuingTemporaryPersonDataDelete(String personObjId){
        if(StringUtils.isNotBlank(personObjId)){
            TemporaryAdmissionBean temporaryAdmissionBean = temporaryAdmissionService.getById(personObjId);
            if(Objects.nonNull(temporaryAdmissionBean)) {
                int personId = temporaryAdmissionBean.getPersonId();

                List<String> workIdList = Lists.newArrayList();
                String orgId = temporaryAdmissionBean.getOrgId();
                // 部门直接查工程
                orgId = orgService.findDepartmentOrgIdByOrgId(orgId);
                List<ConstructionWorkBean> constructionWorkBeanList = constructionWorkService.lambdaQuery().eq(ConstructionWorkBean::getProjectDepartmentObjId, orgId).list();
                if(org.apache.commons.collections.CollectionUtils.isNotEmpty(constructionWorkBeanList)){
                    workIdList = constructionWorkBeanList.stream().map(ConstructionWorkBean::getObjId).collect(Collectors.toList());
                }

                // 参建单位查关联的工程
                workIdList.addAll(constructionWorkService.findWorkIdByUnitId(orgId));

                if(CollectionUtils.isNotEmpty(workIdList)){
                    List<BarriersBean> barriersBeanList = barriersService.findFactorySerialNumberByDistinguishAndWorkIdIn(BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02, workIdList);
                    if(CollectionUtils.isNotEmpty(barriersBeanList)){
                        // 联义设备
                        List<String> lianYiDevSnoList = barriersBeanList.stream().filter(obj -> StringUtils.equals(obj.getBrand(), BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)).map(BarriersBean::getFactorySerialNumber).collect(Collectors.toList());
                        lianYi.issuedPersonData_deletePerson(lianYiDevSnoList, personId);
                    }
                }
            }
        }
    }
    
    /**
     * 同步人员信息-项目部
     * @param personObjId 人员ObjId
     */
    @Override
    public void issuingPersonByPersonObjIdAndDepartment(String personObjId) {
        PersonManageBean personManageBean = personManageService.getById(personObjId);
        if(Objects.nonNull(personManageBean)){
            boolean status = orgService.checkDepartmentByOrgId(personManageBean.getOrgId());
            // 部门人员直接下发，否则参建单位的需要等办理准入后下发
            if(status){
                int personId = personManageBean.getId();
                List<Integer> personIdList = new ArrayList<>();
                personIdList.add(personId);

                String orgId = personManageBean.getOrgId();
                boolean ifDepartment = orgService.checkDepartmentByOrgId(orgId);
                List<String> workIdList;
                if(!ifDepartment){
                    workIdList = personAccessRelaService.findWorkIdByPersonId(personObjId);    
                }else{
                    workIdList = constructionWorkService.findWorkIdByProjectDepartmentObjId(orgId);
                }
                
                if(CollectionUtils.isNotEmpty(workIdList)){
                    List<BarriersBean> barriersBeanList = barriersService.findByWorkIdInAndDistinguish(workIdList, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02, BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
                    if(CollectionUtils.isNotEmpty(barriersBeanList)){
                        barriersBeanList.forEach(barriersBean -> {
                            try {
                                if(Constants.firstSyncByBarrierGatePersonList.contains(barriersBean.getFactorySerialNumber())){
                                    if(StringUtils.equals(barriersBean.getOnlineStatus(), BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01)){
                                        lianYi.issuedPersonData_syncPerson(barriersBean.getFactorySerialNumber(), personIdList, personIdList.size());
                                    }
                                }
                            }catch (Exception e){
                                log.error("人员信息下发失败，{}, devSno:{}", personIdList, barriersBean.getFactorySerialNumber(), e);
                            }

                        });
                    }
                }
            }
        }
    }

    /**
     * 同步人员信息
     * @param personObjId 人员ObjId
     */
    @Override
    public void issuingPersonByPersonObjId(String personObjId) {
        PersonManageBean personManageBean = personManageService.getById(personObjId);
        if(Objects.nonNull(personManageBean)){
            int personId = personManageBean.getId();
            List<Integer> personIdList = new ArrayList<>();
            personIdList.add(personId);

            String orgId = personManageBean.getOrgId();
            boolean ifDepartment = orgService.checkDepartmentByOrgId(orgId);
            List<String> workIdList;
            if(!ifDepartment){
                workIdList = personAccessRelaService.findWorkIdByPersonId(personObjId);
            }else{
                workIdList = constructionWorkService.findWorkIdByProjectDepartmentObjId(orgId);
            }

            if(CollectionUtils.isNotEmpty(workIdList)){
                List<BarriersBean> barriersBeanList = barriersService.findByWorkIdInAndDistinguish(workIdList, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02, BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
                if(CollectionUtils.isNotEmpty(barriersBeanList)){
                    barriersBeanList.forEach(barriersBean -> {
                        try {
                            if(Constants.firstSyncByBarrierGatePersonList.contains(barriersBean.getFactorySerialNumber())){
                                if(StringUtils.equals(barriersBean.getOnlineStatus(), BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01)){
                                    lianYi.issuedPersonData_syncPerson(barriersBean.getFactorySerialNumber(), personIdList, personIdList.size());
                                }
                            }
                        }catch (Exception e){
                            log.error("人员信息下发失败，{}, devSno:{}", personIdList, barriersBean.getFactorySerialNumber(), e);
                        }

                    });
                }
            }
        }
    }

    /**
     * 记录人员打卡信息
     *
     * @param dto 人员信息
     * @return 返回结果
     */
    @Override
    public DeviceNotifyVO recordUploadOnline_lianYi(RecordUploadOnlineDTO dto) {
        DeviceNotifyVO deviceNotifyVO = new DeviceNotifyVO();
        if(dto.getPerson_id() != 0){
            PersonSimpleInfoVO personSimpleInfoVO = personManageService.findById(dto.getPerson_id());
            if(Objects.nonNull(personSimpleInfoVO)){
                AttendanceBean attendanceBean = new AttendanceBean();
                attendanceBean.setObjId(BaseCommonUtils.generateUUID());
                attendanceBean.setCard(personSimpleInfoVO.getCard());
                attendanceBean.setName(personSimpleInfoVO.getName());
                if(StringUtils.isNotBlank(dto.getCapture_time())){
                    long captureTime = Long.parseLong(dto.getCapture_time());
                    ZonedDateTime zonedDateTime = Instant.ofEpochMilli(captureTime)
                            .atZone(ZoneId.systemDefault());
                    // 将时间戳转换为 LocalDate

                    attendanceBean.setCheckinDate(zonedDateTime.toLocalDate());
                    attendanceBean.setCheckinTime(zonedDateTime.toLocalTime());
                }
                attendanceBean.setUnitObjId(personSimpleInfoVO.getOrgId());

                // 设备信息
                if(StringUtils.isNotBlank(dto.getDev_sno())){
                    BarriersBaseVO barriersBaseVO = barriersService.findByFactorySerialNumberAndOnLine(dto.getDev_sno());
                    if(Objects.nonNull(barriersBaseVO)){
                        attendanceBean.setBarriersObjId(barriersBaseVO.getObjId());
                        attendanceBean.setDevSn(dto.getDev_sno());
                        attendanceBean.setConstructionWorkObjId(barriersBaseVO.getConstructionWorkObjId());
                        attendanceBean.setConstructionWorkSiteObjId(barriersBaseVO.getConstructionWorkSiteObjId());
                    }
                }

                // 识别照片
                if(StringUtils.isNotBlank(dto.getCapture_img())){
                    String captureImg = attachmentService.decodeBase64ToImage(dto.getCapture_img(), attendanceBean.getObjId(), personSimpleInfoVO.getOrgId());
                    if(StringUtils.isNotBlank(captureImg)){
                        attendanceBean.setCaptureImg(captureImg);
                    }
                }
                
                boolean status = attendanceService.save(attendanceBean);
                if(status){
                    deviceNotifyVO.setCode(MqttEnum.RequestCode.SUCCESS.getCode());
                    deviceNotifyVO.setMsg(MqttEnum.RequestCode.SUCCESS.getMsg());
                    deviceNotifyVO.setSuccess(true);
                    return deviceNotifyVO;
                }
            }
        }else{
//            AttendanceBean attendanceBean = new AttendanceBean();
//            attendanceBean.setObjId(BaseCommonUtils.generateUUID());
//            attendanceBean.setPersonType(dto.getPerson_type());
//            if(StringUtils.isNotBlank(dto.getCapture_time())){
//                long captureTime = Long.parseLong(dto.getCapture_time());
//                ZonedDateTime zonedDateTime = Instant.ofEpochMilli(captureTime)
//                        .atZone(ZoneId.systemDefault());
//                // 将时间戳转换为 LocalDate
//
//                attendanceBean.setCheckinDate(zonedDateTime.toLocalDate());
//                attendanceBean.setCheckinTime(zonedDateTime.toLocalTime());
//            }
//
//            String orgId = "";
//            // 设备信息
//            if(StringUtils.isNotBlank(dto.getDev_sno())){
//                BarriersBaseVO barriersBaseVO = barriersService.findByFactorySerialNumber(dto.getDev_sno());
//                if(Objects.nonNull(barriersBaseVO)){
//                    attendanceBean.setBarriersObjId(barriersBaseVO.getObjId());
//                    attendanceBean.setDevSn(dto.getDev_sno());
//                    attendanceBean.setConstructionWorkObjId(barriersBaseVO.getConstructionWorkObjId());
//                    attendanceBean.setConstructionWorkSiteObjId(barriersBaseVO.getConstructionWorkSiteObjId());
//                    
//                    if(StringUtils.isNotBlank(barriersBaseVO.getConstructionWorkObjId())){
//                        orgId = constructionWorkService.findProjectDepartmentObjIdByObjId(barriersBaseVO.getConstructionWorkObjId());
//                    }
//                }
//            }
//
//            // 识别照片
//            if(StringUtils.isNotBlank(dto.getCapture_img())){
//                String captureImg = attachmentService.decodeBase64ToImage(dto.getCapture_img(), attendanceBean.getObjId(), orgId);
//                if(StringUtils.isNotBlank(captureImg)){
//                    attendanceBean.setCaptureImg(captureImg);
//                }
//            }
//
//            boolean status = attendanceService.save(attendanceBean);
//            if(status){
//                deviceNotifyVO.setCode(MqttEnum.RequestCode.SUCCESS.getCode());
//                deviceNotifyVO.setMsg(MqttEnum.RequestCode.SUCCESS.getMsg());
//                deviceNotifyVO.setSuccess(true);
//                return deviceNotifyVO;
//            }
            log.info("人员记录：personType:{}", dto.getPerson_type());
            deviceNotifyVO.setCode(MqttEnum.RequestCode.SUCCESS.getCode());
            deviceNotifyVO.setMsg(MqttEnum.RequestCode.SUCCESS.getMsg());
            deviceNotifyVO.setSuccess(true);
            return deviceNotifyVO;
        }
        deviceNotifyVO.setCode(MqttEnum.RequestCode.RECORD_ONLINE.getCode());
        deviceNotifyVO.setMsg(MqttEnum.RequestCode.RECORD_ONLINE.getMsg());
        deviceNotifyVO.setSuccess(true);
        return deviceNotifyVO;
    }

    /**
     * 下发数据-临时准入
     *
     * @param personObjId 人员ID
     */
    @Override
    public void issuingTemporaryPersonByPersonObjId(String personObjId) {
        TemporaryAdmissionBean temporaryAdmissionBean = temporaryAdmissionService.getById(personObjId);
        if(Objects.nonNull(temporaryAdmissionBean)){
            if(StringUtils.isNotBlank(temporaryAdmissionBean.getAccessMonadObjId())){
                ConstructionWorkAccessMonadBean workAccessMonadBean = constructionWorkAccessMonadService.findByObjId(temporaryAdmissionBean.getAccessMonadObjId());
                if(Objects.nonNull(workAccessMonadBean) && StringUtils.equals(workAccessMonadBean.getAccessStatus(), BaseCodeConstants.CodeType_AccessStatus.KEY_AccessStatus01)){
                    Date cycleStart = workAccessMonadBean.getCycleStart();
                    Date cycleEnd = workAccessMonadBean.getCycleEnd();

                    DateTime dateTime = DateUtil.beginOfDay(DateUtil.date());
                    // 到达准入周期，则下发
                    if(cycleStart.getTime() <= dateTime.getTime() && cycleEnd.getTime() >= dateTime.getTime()){
                        int personId = temporaryAdmissionBean.getPersonId();
                        List<Integer> personIdList = new ArrayList<>();
                        personIdList.add(personId);

                        List<String> workIdList = Lists.newArrayList();
                        workIdList.add(workAccessMonadBean.getConstructionWorkObjId());

                        if(CollectionUtils.isNotEmpty(workIdList)){
                            List<BarriersBean> barriersBeanList = barriersService.findByWorkIdInAndDistinguish(workIdList, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02, BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
                            if(CollectionUtils.isNotEmpty(barriersBeanList)){
                                barriersBeanList.forEach(barriersBean -> {
                                    try {
                                        if(Constants.firstSyncByBarrierGatePersonList.contains(barriersBean.getFactorySerialNumber())){
                                            if(StringUtils.equals(barriersBean.getOnlineStatus(), BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01)){
                                                lianYi.issuedPersonData_syncPerson(barriersBean.getFactorySerialNumber(), personIdList, personIdList.size());
                                            }
                                        }
                                    }catch (Exception e){
                                        log.error("人员信息下发失败，{}, devSno:{}", personIdList, barriersBean.getFactorySerialNumber(), e);
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 下发车辆
     * @param licensePlate 车牌号
     * @param cycleStart 开始时间
     * @param cycleEnd 结束时间
     * @param workId 工程ID
     */
    @Override
    public void issuedCarAdd(String licensePlate, Date cycleStart, Date cycleEnd, String workId){
        CompletableFuture.runAsync(() -> {
            boolean ifBlacklist = blacklistService.checkBlackListByIdNumberAndType(licensePlate, BaseCodeConstants.CodeType_ApprovalFormType.KEY_ApprovalFormType02);

            // 装载数据
            VehicleSyncDTO vehicleSyncDTO = new VehicleSyncDTO();
            vehicleSyncDTO.setLicensePlate(licensePlate);
            vehicleSyncDTO.setIfDepartment(BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode02);
            vehicleSyncDTO.setIfBlacklist(ifBlacklist ? BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01 : BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode02);
            vehicleSyncDTO.setCycleStart(cycleStart);
            vehicleSyncDTO.setCycleEnd(cycleEnd);


            List<BarriersBean> barriersBeanList = barriersService.findByWorkIdInAndDistinguish(Collections.singletonList(workId), BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType01, BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
            if(CollectionUtils.isNotEmpty(barriersBeanList)){
                barriersBeanList.forEach(barriersBean -> {
                    if(StringUtils.equals(barriersBean.getBrand(), BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)){
                        lianYi.issuedCarBySn(barriersBean.getFactorySerialNumber(), vehicleSyncDTO);
                    }
                });
            }
            
            
        });
    }

    /**
     * 下发车辆数据-临时准入
     *
     * @param carId 车辆ID
     */
    @Override
    public void issuingTemporaryCarToBarrierGateByCarObjId(String carId) {
        TemporaryAdmissionBean temporaryAdmissionBean = temporaryAdmissionService.getById(carId);
        if(Objects.nonNull(temporaryAdmissionBean)){
            if(StringUtils.isNotBlank(temporaryAdmissionBean.getAccessMonadObjId())) {
                ConstructionWorkAccessMonadBean workAccessMonadBean = constructionWorkAccessMonadService.findByObjId(temporaryAdmissionBean.getAccessMonadObjId());
                if (Objects.nonNull(workAccessMonadBean) && StringUtils.equals(workAccessMonadBean.getAccessStatus(), BaseCodeConstants.CodeType_AccessStatus.KEY_AccessStatus01)) {

                    Date cycleStart = workAccessMonadBean.getCycleStart();
                    Date cycleEnd = workAccessMonadBean.getCycleEnd();

                    DateTime dateTime = DateUtil.beginOfDay(DateUtil.date());
                    if(cycleStart.getTime() <= dateTime.getTime() && cycleEnd.getTime() >= dateTime.getTime()){
                        issuedCarAdd(temporaryAdmissionBean.getIdNumber(), workAccessMonadBean.getCycleStart(), workAccessMonadBean.getCycleEnd(), workAccessMonadBean.getConstructionWorkObjId());    
                    }
                }
            }
        }
    }

    /**
     * 删除车辆
     * @param carId 临时车辆ID
     */
    @Override
    public void issuingTemporaryCarDeleteToBarrierGateByCarObjId(String carId) {
        TemporaryAdmissionBean temporaryAdmissionBean = temporaryAdmissionService.getById(carId);
        if(Objects.nonNull(temporaryAdmissionBean)){
            if(StringUtils.isNotBlank(temporaryAdmissionBean.getAccessMonadObjId())) {
                issuedCarDeleteByLicensePlate(temporaryAdmissionBean.getIdNumber(), Collections.singletonList(temporaryAdmissionBean.getConstructionWorkObjId()));
            }
        }
    }

    /**
     * 下发车辆删除
     * @param licensePlate 车牌号
     * @param workIdList 工程
     */
    public void issuedCarDeleteByLicensePlate(String licensePlate, List<String> workIdList){
        List<BarriersBean> barriersBeanList = barriersService.findByWorkIdInAndDistinguish(workIdList, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType01, BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
        if(CollectionUtils.isNotEmpty(barriersBeanList)){
            barriersBeanList.forEach(barriersBean -> {
                if(StringUtils.equals(barriersBean.getBrand(), BaseCodeConstants.CodeType_BrandType.KEY_BrandType03)){
                    lianYi.issuedListDeleteByLicensePlate(barriersBean.getFactorySerialNumber(), licensePlate);
                }
            });
        }
    }

    /**
     * 下发车辆
     * @param sn 设备号
     */
    @Override
    public void issuedCarBySn(String sn) {
        BarriersBaseVO barriersBaseVO = barriersService.findByFactorySerialNumberAndOnLine(sn);
        if(Objects.nonNull(barriersBaseVO)){
            if(StringUtils.isEmpty(barriersBaseVO.getConstructionWorkObjId())){
                log.error("下发车辆-所属工程ID为空,设备号：{}", sn);
                return;
            }
            
            // 查询待下发的车牌
            Date date = DateUtil.beginOfDay(DateUtil.date());
            List<VehicleSyncDTO> syncDTOList = vehicleService.findLicensePlateByWorkIdAndNotSync(sn, barriersBaseVO.getConstructionWorkObjId(), date);
            if(CollectionUtils.isEmpty(syncDTOList)){
                log.error("下发车辆-不存在待下发的车牌,设备号：{}, 工程：{}", sn, barriersBaseVO.getConstructionWorkObjId());
                return;
            }

            // 查询是否是没名单
            List<String> licensePlateList = syncDTOList.stream().map(VehicleSyncDTO::getLicensePlate).collect(Collectors.toList());
            List<String> blacklist = blacklistService.findBlacklistByIdNumberInAndType(licensePlateList, BaseCodeConstants.CodeType_ApprovalFormType.KEY_ApprovalFormType02);
            blacklist = blacklist == null ? Lists.newArrayList() : blacklist;
            List<String> finalBlacklist = blacklist;
            syncDTOList.forEach(syncDTO -> syncDTO.setIfBlacklist(finalBlacklist.contains(syncDTO.getLicensePlate()) ? BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01 : BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode02));
            
            if(StringUtils.equals(barriersBaseVO.getBrand(), BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)){
                // 联义
                lianYi.issuedCarBySn(sn, syncDTOList);
            }
        }
    }

    /**
     * 下发车辆
     * @param vehicleId 车辆ID
     */
    @Override
    public void issuedCarByVehicleId(String vehicleId) {
        VehicleInfoBean vehicleInfoBean = vehicleService.getById(vehicleId);
        if(Objects.nonNull(vehicleInfoBean)){
            String organization = vehicleInfoBean.getOrganization();
            boolean status = orgService.checkDepartmentByOrgId(organization);
            List<String> workIdList = Lists.newArrayList();
            Map<String, List<VehicleAccessRelaBean>> accessRelaBeanMap = Maps.newHashMap();
            if(status) {
                workIdList = constructionWorkService.findWorkIdByProjectDepartmentObjId(organization);
            }else{
                List<VehicleAccessRelaBean> accessRelaBeanList = vehicleAccessRelaService.findAccessInfoByVehicleIdAndCycleEndGe(vehicleId, DateUtil.endOfDay(new Date()));
                if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(accessRelaBeanList)){
                    workIdList = accessRelaBeanList.stream()
                            .map(VehicleAccessRelaBean::getConstructionWorkObjId)
                            .distinct().collect(Collectors.toList());
                    accessRelaBeanMap = accessRelaBeanList.stream().collect(Collectors.groupingBy(VehicleAccessRelaBean::getVehicleId));
                }
            }

            // 是否黑名单
            boolean ifBlacklist = blacklistService.checkBlackListByIdNumberAndType(vehicleInfoBean.getLicensePlate(), BaseCodeConstants.CodeType_ApprovalFormType.KEY_ApprovalFormType02);

            if(CollectionUtils.isNotEmpty(workIdList)) {
                List<BarriersBean> barriersBeanList = barriersService.findByWorkIdInAndDistinguish(workIdList, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType01, BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
                if (CollectionUtils.isNotEmpty(barriersBeanList)) {
                    Map<String, List<VehicleAccessRelaBean>> finalAccessRelaBeanMap = accessRelaBeanMap;
                    barriersBeanList.forEach(barriersBean -> {
                        VehicleSyncDTO vehicleSyncDTO = new VehicleSyncDTO();
                        vehicleSyncDTO.setLicensePlate(vehicleInfoBean.getLicensePlate());
                        vehicleSyncDTO.setIfBlacklist(ifBlacklist ? BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01 : BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode02);
                        vehicleSyncDTO.setIfDepartment(status ? BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01 : BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode02);
                        
                        if (finalAccessRelaBeanMap.containsKey(vehicleId)) {
                            List<VehicleAccessRelaBean> vehicleAccessRelaBeans = finalAccessRelaBeanMap.get(vehicleId);
                            vehicleAccessRelaBeans = vehicleAccessRelaBeans.stream().filter(obj -> obj.getConstructionWorkObjId().equals(barriersBean.getConstructionWorkObjId())).collect(Collectors.toList());
                            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(vehicleAccessRelaBeans)) {
                                VehicleAccessRelaBean vehicleAccessRelaBean = vehicleAccessRelaBeans.get(0);

                                vehicleSyncDTO.setCycleStart(vehicleAccessRelaBean.getCycleStart());
                                vehicleSyncDTO.setCycleEnd(vehicleAccessRelaBean.getCycleEnd());
                            }
                        }
                        
                        if(StringUtils.equals(barriersBean.getBrand(), BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)){
                            // 联义
                            lianYi.issuedCarBySn(barriersBean.getFactorySerialNumber(), vehicleSyncDTO);
                        }
                    });
                }
            }
        }
    }

    /**
     * 下发数据到道闸
     *
     * @param nowDate 校验时间
     */
    @Override
    public void taskIssuedDataToBarrierGates(LocalDateTime nowDate) {
        List<BarriersBean> barriersBeanList = barriersService.findAllByOnLineStatus(BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01);
        if(CollectionUtils.isNotEmpty(barriersBeanList)){
            barriersBeanList.forEach(barriersBean -> {
                String devSno = barriersBean.getFactorySerialNumber();
                String constructionWorkObjId = barriersBean.getConstructionWorkObjId();
                String brand = barriersBean.getBrand();
                ConstructionWorkBean constructionWorkBean = constructionWorkService.getById(constructionWorkObjId);
                if(Objects.nonNull(constructionWorkBean) && StringUtils.isNotBlank(constructionWorkBean.getProjectDepartmentObjId())) {
                    String orgId = constructionWorkBean.getProjectDepartmentObjId();
                    // 下发人员
                    taskIssuedPersonDataToBarrierGates(nowDate, brand, devSno, orgId, constructionWorkObjId);
                    
                    // 下发车辆
                    taskIssuedCarDataToBarrierGates(nowDate, brand, devSno, orgId, constructionWorkObjId);
                }
            });
        }
    }

    /**
     * 下发数据到道闸设备--人员
     * @param nowDate 时间
     * @param brand 设备类别
     * @param devSno 设备号
     * @param orgId 组织机构ID
     * @param constructionWorkObjId 施工工程ID
     */
    private void taskIssuedPersonDataToBarrierGates(LocalDateTime nowDate, String brand, String devSno, String orgId, String constructionWorkObjId){
       
        if(StringUtils.equals(brand, BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)){
            // 联义
            List<String> orgIdList = null;
            try {
                orgIdList = orgService.findOrgTreeIdByOrgId(orgId);
            } catch (GeneralSimpleException e) {
                log.error("下发数据到道闸，查询单位信息异常", e);
                return;
            }
            if(CollectionUtils.isEmpty(orgIdList)){
                log.error("下发数据到道闸，设备未查询到单位信息：devSno：{}， brand:{}, constructionWorkObjId：{}，ordId:{}", devSno, brand, constructionWorkObjId, orgId);
                return;
            }
            // 所有需要同步的人员ID
            Date date = Date.from(nowDate.atZone(ZoneId.systemDefault()).toInstant());
            List<Integer> allPersonIdList = personManageService.findPersonIdByWorkId(devSno, orgIdList, constructionWorkObjId, date);
            if(CollectionUtils.isEmpty(allPersonIdList)){
                log.error("下发数据到道闸，设备未查询到待下发的人员：devSno：{}， brand:{}, constructionWorkObjId：{}，ordId:{}", devSno, brand, constructionWorkObjId, orgId);
                return;
            }

            // 已经同步，并且在有效期的人员
            List<Integer> personIdByExistEffective = personManageService.findPersonIdByExistEffective(devSno, orgIdList, constructionWorkObjId, date);
            if(CollectionUtils.isNotEmpty(personIdByExistEffective)){
                // 存在有效期的用户 - 已经同步的并且存在有效期的用户 = 待同步的存在有效期的用户
                allPersonIdList.removeAll(personIdByExistEffective);
            }

            // 下发人员数据
            if(allPersonIdList.size() > Constants.PERSON_SYNC_LIMIT){
                List<List<Integer>> allPersonIdPage = ListUtils.partition(allPersonIdList, Constants.PERSON_SYNC_LIMIT);
                allPersonIdPage.forEach(obj -> lianYi.issuedPersonData_syncPerson(devSno, obj, obj.size()));
            }else{
                lianYi.issuedPersonData_syncPerson(devSno, allPersonIdList, allPersonIdList.size());
            }
        }
    }

    /**
     * 下发数据到道闸设备--人员
     * @param nowDate 时间
     * @param brand 设备类别
     * @param devSno 设备号
     * @param orgId 组织机构ID
     * @param constructionWorkObjId 施工工程ID
     */
    private void taskIssuedCarDataToBarrierGates(LocalDateTime nowDate, String brand, String devSno, String orgId, String constructionWorkObjId){

        if(StringUtils.equals(brand, BaseCodeConstants.CodeType_BrandType.KEY_BrandType04)) {
            // 联义
            List<String> orgIdList = null;
            try {
                orgIdList = orgService.findOrgTreeIdByOrgId(orgId);
            } catch (GeneralSimpleException e) {
                log.error("下发数据到道闸，查询单位信息异常", e);
                return;
            }
            if(CollectionUtils.isEmpty(orgIdList)){
                log.error("下发数据到道闸，设备未查询到单位信息：devSno：{}， brand:{}, constructionWorkObjId：{}，ordId:{}", devSno, brand, constructionWorkObjId, orgId);
                return;
            }

            Date date = Date.from(nowDate.atZone(ZoneId.systemDefault()).toInstant());
            
            // 查询全部，有准入期的车牌
            List<VehicleSyncDTO> allVehicleSyncDTOList = vehicleService.findLicensePlateByWorkId(devSno, constructionWorkObjId, orgIdList, date, false);
            if (CollectionUtils.isEmpty(allVehicleSyncDTOList)) {
                log.error("下发车辆-不存在待下发的车牌,设备号：{}, 工程：{}", devSno, constructionWorkObjId);
                return;
            }

            List<VehicleSyncDTO> syncDTOList = vehicleService.findLicensePlateByWorkId(devSno, constructionWorkObjId, orgIdList, date, true);
            if(CollectionUtils.isNotEmpty(syncDTOList)){
                List<String>  syncLicensePlateList = syncDTOList.stream().map(VehicleSyncDTO::getLicensePlate).distinct().collect(Collectors.toList());

                allVehicleSyncDTOList = allVehicleSyncDTOList.stream().filter(obj -> !syncLicensePlateList.contains(obj.getLicensePlate())).collect(Collectors.toList());
                if(CollectionUtils.isEmpty(allVehicleSyncDTOList)){
                    log.info("下发车辆-不存在需要下发的车辆信息：devSno：{}， brand:{}, constructionWorkObjId：{}，ordId:{}", devSno, brand, constructionWorkObjId, orgId);
                    return;
                }
            }
            
            // 查询是否是没名单
            List<String> licensePlateList = allVehicleSyncDTOList.stream().map(VehicleSyncDTO::getLicensePlate).collect(Collectors.toList());
            List<String> blacklist = blacklistService.findBlacklistByIdNumberInAndType(licensePlateList, BaseCodeConstants.CodeType_ApprovalFormType.KEY_ApprovalFormType02);
            blacklist = blacklist == null ? Lists.newArrayList() : blacklist;
            List<String> finalBlacklist = blacklist;
            allVehicleSyncDTOList.forEach(syncDTO -> syncDTO.setIfBlacklist(finalBlacklist.contains(syncDTO.getLicensePlate()) ? BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode01 : BaseCodeConstants.CodeType_WhetherCode.KEY_WhetherCode02));
            
            // 下发人员数据
            if(allVehicleSyncDTOList.size() > Constants.CAR_SYNC_LIMIT){
                List<List<VehicleSyncDTO>> listList = ListUtils.partition(allVehicleSyncDTOList, Constants.PERSON_SYNC_LIMIT);
                listList.forEach(obj -> lianYi.issuedCarBySn(devSno, obj));
            }else{
                lianYi.issuedCarBySn(devSno, allVehicleSyncDTOList);
            }
           
        }
    }
}
